{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Why policy-based methods?\n",
    "\n",
    "The objective of reinforcement learning is to find the optimal policy, which is the\n",
    "policy that provides the maximum return. So far, we have learned several different\n",
    "algorithms for computing the optimal policy, and all these algorithms have been\n",
    "value-based methods. Wait, what are value-based methods? Let's recap what value-\n",
    "based methods are, and the problems associated with them, and then we will learn\n",
    "about policy-based methods. Recapping is always good, isn't it?\n",
    "\n",
    "With value-based methods, we extract the optimal policy from the optimal Q\n",
    "function (Q values), meaning we compute the Q values of all state-action pairs to\n",
    "find the policy. We extract the policy by selecting an action in each state that has the\n",
    "maximum Q value. For instance, let's say we have two states $s_0$ and $s_1$ and our action\n",
    "space has two actions; let the actions be 0 and 1. First, we compute the Q value of all\n",
    "the state-action pairs, as shown in the following table. Now, we extract policy from\n",
    "the Q function (Q values) by selecting action 0 in state $s_0$ and action 1 in state $s_1$ as\n",
    "they have the maximum Q value:\n",
    "\n",
    "![title](Images/1.png)\n",
    "\n",
    "\n",
    "\n",
    "Later, we learned that it is difficult to compute the Q function when our environment\n",
    "has a large number of states and actions as it would be expensive to compute the\n",
    "Q values of all possible state-action pairs. So, we resorted to the Deep Q Network\n",
    "(DQN). In DQN, we used a neural network to approximate the Q function (Q value).\n",
    "Given a state, the network will return the Q values of all possible actions in that\n",
    "state. For instance, consider the grid world environment. Given a state, our DQN will\n",
    "return the Q values of all possible actions in that state. Then we select the action that\n",
    "has the highest Q value. As we can see in the following figure, given state E, DQN returns the\n",
    "Q value of all possible actions (up, down, left, right). Then we select the right action in\n",
    "state E since it has the maximum Q value:\n",
    "\n",
    "![title](Images/2.png)\n",
    "\n",
    "\n",
    "Thus, in value-based methods, we improve the Q function iteratively, and once we\n",
    "have the optimal Q function, then we extract optimal policy by selecting the action\n",
    "in each state that has the maximum Q value.\n",
    "\n",
    "One of the disadvantages of the value-based method is that it is suitable only for\n",
    "discrete environments (environments with a discrete action space), and we cannot\n",
    "apply value-based methods in continuous environments (environments with a\n",
    "continuous action space).\n",
    "\n",
    "We have learned that a discrete action space has a discrete set of actions; for example,\n",
    "the grid world environment has discrete actions (up, down, left, and right) and the\n",
    "continuous action space consists of actions that are continuous values, for example,\n",
    "controlling the speed of a car.\n",
    "\n",
    "So far, we have only dealt with a discrete environment where we had a discrete\n",
    "action space, so we easily computed the Q value of all possible state-action pairs.\n",
    "But how can we compute the Q value of all possible state-action pairs when our\n",
    "action space is continuous? Say we are training an agent to drive a car and say we\n",
    "have one continuous action in our action space. Let the action be the speed of the\n",
    "car and the value of the speed of the car ranges from 0 to 150 kmph. In this case,\n",
    "how can we compute the Q value of all possible state-action pairs with the action\n",
    "being a continuous value?\n",
    "\n",
    "In this case, we can discretize the continuous actions into speed (0 to 10) as action\n",
    "1, speed (10 to 20) as action 2, and so on. After discretization, we can compute the\n",
    "Q value of all possible state-action pairs. However, discretization is not always\n",
    "desirable. We might lose several important features and we might end up in an\n",
    "action space with a huge set of actions.\n",
    "\n",
    "\n",
    "Most real-world problems have continuous action space, say, a self-driving car, or\n",
    "a robot learning to walk and more. Apart from having a continuous action space they\n",
    "also have a high dimension. Thus, the DQN and other value-based methods cannot\n",
    "deal with the continuous action space effectively.\n",
    "\n",
    "So, we use the policy-based methods. With policy-based methods, we don't need\n",
    "to compute the Q function (Q values) to find the optimal policy; instead, we can\n",
    "compute them directly. That is, we don't need the Q function to extract the policy.\n",
    "Policy-based methods have several advantages over value-based methods, and they\n",
    "can handle both discrete and continuous action spaces.\n",
    "\n",
    "We learned that DQN takes care of the exploration-exploitation dilemma by using\n",
    "the epsilon-greedy policy. With the epsilon-greedy policy, we either select the best\n",
    "action with the probability 1-epsilon or a random action with the probability epsilon.\n",
    "Most policy-based methods use a stochastic policy. We know that with a stochastic\n",
    "policy, we select actions based on the probability distribution over the action\n",
    "space, which allows the agent to explore different actions instead of performing the\n",
    "same action every time. Thus, policy-based methods take care of the exploration-\n",
    "exploitation trade-off implicitly by using a stochastic policy. However, there are\n",
    "several policy-based methods that use a deterministic policy as well. We will learn\n",
    "more about them in the upcoming chapters.\n",
    "\n",
    "Okay, how do policy-based methods work, exactly? How do they find an optimal\n",
    "policy without computing the Q function? We will learn about this in the next\n",
    "section. Now that we have a basic understanding of what a policy gradient\n",
    "method is, and also the disadvantages of value-based methods, in the next section\n",
    "we will learn about a fundamental and interesting policy-based method called\n",
    "policy gradient."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
